package com.hy.lightning.ds.jdbc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class MapUtils {

	public static void main(String[] args) throws Exception {
		
	}

	public static class ResultBean {
		String sql;
		
		Object[] params;
		
		public ResultBean(String sql, Object[] params) {
			this.sql = sql;
			this.params = params;
		}

		public String getSql() {
			return sql;
		}

		public void setSql(String sql) {
			this.sql = sql;
		}

		public Object[] getParams() {
			return params;
		}

		public void setParams(Object[] params) {
			this.params = params;
		}
		
		@Override
		public String toString() {
			return sql +":"+ Arrays.deepToString(params);
		}
	}
	
	static class Pair {
		String[] keys;
		Object[] values;
		public Pair(Map<String, Object> map) {
			List<String> keyList = new ArrayList<>();
			List<Object> valueList = new ArrayList<>();
			for (Entry<String, Object> entry : map.entrySet()) {
				String key = entry.getKey();
				Object value = entry.getValue();
				if ("id".equals(key) || value == null)
					continue;
				keyList.add(key);
				valueList.add(value);
			}
			if(map.get("id") != null) {
				keyList.add("id");
				valueList.add(map.get("id"));
			}
			keys = keyList.toArray(new String[keyList.size()]);
			values = valueList.toArray(new Object[valueList.size()]);
		}

		public Object[] getValues(String[] keys, Map<String, Object> map) {
			values = new Object[keys.length];
			int i = 0;
			for (String key : keys) {
				values[i] = map.get(key);
				i++;
			}
			return values;
		}
	}
	
	public static ResultBean getInsertSqlByMap(String tableName, Map<String, Object> map) {
		if(!map.containsKey("id")) throw new IllegalAccessError("执行map更新是id不能为空");
		Pair pair = new Pair(map);
		String[] keys = pair.keys;
		String[] newKeys = new String[keys.length];
		for(int i = 0; i < keys.length; i++) {
			newKeys[i] = String.format("`%s`", keys[i]);
		}
		String fileds = Arrays.deepToString(newKeys).replace("[", "(").replace("]", ")");
		String[] vaules = new String[pair.keys.length];
		for (int i = 0; i < vaules.length; i++) {
			vaules[i] = "?";
		}
		String params = Arrays.deepToString(vaules).replace("[", "(").replace("]", ")");
		String sql = "insert into `%s`%s values %s ";
		sql =  String.format(sql, tableName, fileds, params);
		return new ResultBean(sql, pair.values);
	}

	public static List<ResultBean> getUpdateBatchSqlByMaps(String tableName, List<Map<String,Object>> list) {
		List<ResultBean> relList = new ArrayList<>();
		for(Map<String,Object> map : list) {
			ResultBean enBean = getUpdateSqlByMap(tableName, map);
			relList.add(enBean);
		}
		return relList;
	}
	
	public static List<ResultBean> getInsertBatchSqlByMaps(String tableName, List<Map<String,Object>> list) {
		List<ResultBean> relList = new ArrayList<>();
		for(Map<String,Object> map : list) {
			ResultBean enBean = getInsertSqlByMap(tableName, map);
			relList.add(enBean);
		}
		return relList;
	}
	
	public static List<ResultBean> getDeleteBatchSqlByMaps(String tableName, List<Map<String,Object>> list) {
		List<ResultBean> relList = new ArrayList<>();
		for(Map<String,Object> map : list) {
			ResultBean enBean = getDeleteSqlByMap(tableName, map);
			relList.add(enBean);
		}
		return relList;
	}
	
	public static ResultBean getUpdateSqlByMap(String tableName, Map<String,Object> map) {
		if(!map.containsKey("id")) throw new IllegalAccessError("执行map更新是id不能为空");
		Pair pair = new Pair(map);
		StringBuilder sqlTemp = new StringBuilder("UPDATE `" + tableName + "` SET ");
		for (String key : pair.keys) {
			if ("id".equals(key))
				continue;
			sqlTemp.append(String.format("`%s`=?,", key));
		}
		String sql = sqlTemp.toString();
		sql = sql.substring(0, sql.length() - 1) + " where id=?";
		return new ResultBean(sql, pair.values);
	}
	
	public static ResultBean getDeleteSqlByMap(String tableName, Map<String,Object> map) {
		if(!map.containsKey("id")) throw new IllegalAccessError("执行map更新是id不能为空");
		Pair pair = new Pair(map);
		StringBuilder sqlTemp = new StringBuilder("DELETE FROM `" + tableName + "` WHERE 1=1 AND ");
		for (String key : pair.keys) {
			sqlTemp.append(String.format("`%s`=? AND ", key));
		}
		String sql = sqlTemp.toString().trim();
		sql = sql.substring(0, sql.lastIndexOf("AND"));
		return new ResultBean(sql, pair.values);
	}
}
